package entity;
import java.util.Random;
import boundary.Adapter;
import desktop_resources.GUI;

public class GameBoard {

	public static Field[] field;
	private int[] cnsq, price;
	private String[] title, desc, subtitle;
	private desktop_fields.Field[] guiField;
	private Field[] tempField;

	/**
	 * Metode der opretter alle felter med data fundet i arrays fra Text.java
	 * Felterne oprettes i løkker for hver type felt
	 * Alle felterne ligges i et array af Fields som kan hentes med get-metode.
	 */
	public void feltSetup(){
		cnsq = Text.getConsequences();
		price = Text.getPrices();
		title = Adapter.getFieldTitle();
		desc = Adapter.getFieldDesc();
		subtitle = Adapter.getFieldSubtitle();
		tempField = new Field[21]; // array af felter.
		field = new Field[22]; // array af felter.
		guiField = new desktop_fields.Field[22]; // array af gui felter.

		field[0] = new Refuge(title[0], subtitle[0], desc[0], cnsq[0]); // Opretter Startfeltet
		int n;
		for (n=0; n<11; n++){ //opretter Territory felter.
			tempField[n] = new Territory(title[n+1], subtitle[n+1], desc[n+1], price[n+1], cnsq[n+1]);
		}
		for (n=11; n<13; n++){ // opretter Refuge felter.
			tempField[n] = new Refuge(title[n+1], subtitle[n+1], desc[n+1], cnsq[n+1]);
		}
		for (n=13; n<15; n++){ // opretter LaborCamp felter.
			tempField[n] = new LaborCamp(title[n+1], subtitle[n+1], desc[n+1], price[n+1], cnsq[n+1]);
		}
		for (n=15; n<17; n++){ // opretter Tax felter.
			tempField[n] = new Tax(title[n+1], subtitle[n+1], desc[n+1], cnsq[n+1]);
		}
		for (n=17; n<21; n++){ // opretter Fleet felter
			tempField[n] = new Fleet(title[n+1], subtitle[n+1], desc[n+1], price[n+1], cnsq[n+1]);
		}

		// kopierer tempField over i field startende fra plads nummer 1.
		System.arraycopy(tempField, 0, field, 1, tempField.length);
		
	}

	/**
	 * Udskriver information om felterne i array af Field
	 */
	public String toString(){
		String output = "";
		for(int i=0; i < field.length; i++){
			output += field[i].toString() + "\n";
		}
		return output;
	}

	/**
	 * Henter array af GUI Field
	 * @return Array af GUI Field
	 */
	public desktop_fields.Field[] getGuiFields(){
		return guiField;
	}

	/**
	 * Metode til at flytte rundt på feltelementerne i et array og arrangerer dem tilfældigt.
	 * @param tempField Arrayet med elementerne der skal flyttes rundt.
	 */
	public void shuffleArray(){
		Random rnd = new Random();
		for (int i = tempField.length - 1; i > 0; i--){
			int index = rnd.nextInt(i + 1);
			// Simple swap
			Field a = tempField[index];
			tempField[index] = tempField[i];
			tempField[i] = a;
		}		
		// kopierer tempField over i field startende fra plads nummer 1.
		System.arraycopy(tempField, 0, field, 1, tempField.length);
	}
	
	/**
	 * Opretter GUI'en vha. array af desktop_fields.Field (GUI.Field)
	 * @param guiField Array af desktop_fields.Field[]
	 */
	public void createGUI(desktop_fields.Field[] guiField){
		GUI.create(guiField);
	}
	
	/**
	 * kører metoden createGuiField for alle felter i field og gemmer dem i guiField.
	 * @return guiField af typen desktop_fields.Field[]
	 */
	public void createGuiFields(){
		for(int b=0;b<field.length;b++){
			guiField[b]=field[b].createGuiField();
		}
	}
}